﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package reprise.utils {
    import reprise.data.*;

    public class PathUtil {

        public static function pathWithComponents(_arg1:Array):String{
            var _local2:String = _arg1[0];
            var _local3 = 1;
            while (_local3 < _arg1.length) {
                _local2 = stringByAppendingPathComponent(_local2, _arg1[_local3]);
                _local3++;
            };
            return (_local2);
        }
        public static function stringByStandardizingPath(_arg1:String):String{
            var _local4:int;
            _arg1 = _arg1.split("//").join("/").split("//").join("/").split("/./").join("/");
            var _local2:Range = new Range(0, _arg1.length);
            var _local3:int = StringUtil.indexOfStringInRange(_arg1, "../", _local2);
            while ((_local3 - _local2.location) == 0) {
                _local2.location = (_local2.location + 3);
                _local2.length = (_arg1.length - _local2.location);
                _local3 = StringUtil.indexOfStringInRange(_arg1, "../", _local2);
            };
            _local3 = StringUtil.indexOfStringInRange(_arg1, "/../", _local2);
            while (_local3 != -1) {
                _local4 = StringUtil.indexOfStringInRange(_arg1, "/", new Range(0, _local3), StringUtil.BACKWARDS_SEARCH);
                if (_local4 == -1){
                    _arg1 = StringUtil.stringByDeletingCharactersInRange(_arg1, new Range(0, 3));
                } else {
                    _arg1 = StringUtil.stringByDeletingCharactersInRange(_arg1, new Range(_local4, ((_local3 - _local4) + 3)));
                };
                _local2.length = (_arg1.length - _local2.location);
                _local3 = StringUtil.indexOfStringInRange(_arg1, "/../", _local2);
            };
            return (_arg1);
        }
        public static function relativePathToFilename(_arg1:String, _arg2:String):String{
            var _local9:String;
            _arg2 = stringByStandardizingPath(_arg2);
            var _local3:Array = commonRootPathComponents(stringByStandardizingPath(_arg1), _arg2);
            if (_local3 == null){
                return (_arg2);
            };
            var _local4:Array = pathComponents(_arg1).slice((_local3.length - 1));
            var _local5:Array = pathComponents(_arg2).slice((_local3.length - 1));
            var _local6:int = _local4.length;
            if (_local4[(_local4.length - 1)] == ""){
                _local6--;
            };
            if (_local6 < 1){
                return (_local5.join("/"));
            };
            var _local7:Array = _local5.concat();
            var _local8:int;
            while (_local8 < _local6) {
                _local9 = _local4[_local8];
                if (_local9 == ".."){
                    if (_local7[0] == ".."){
                        _local7.shift();
                    } else {
                        return (null);
                    };
                } else {
                    _local7.unshift("..");
                };
                _local8++;
            };
            return (stringByStandardizingPath(pathWithComponents(_local7)));
        }
        public static function stringByDeletingLastPathComponent(_arg1:String):String{
            var _local2:int = _arg1.lastIndexOf(lastPathComponent(_arg1));
            if (_local2 == -1){
                return (_arg1);
            };
            if (_local2 == 0){
                return ("");
            };
            if (_local2 > 1){
                return (_arg1.substring(0, (_local2 - 1)));
            };
            return ("/");
        }
        public static function isAbsolutePath(_arg1:String):Boolean{
            return ((_arg1.charAt(0) == "/"));
        }
        public static function commonRootPathComponents(_arg1:String, _arg2:String):Array{
            var _local3:Array = pathComponents(_arg1);
            var _local4:Array = pathComponents(_arg2);
            var _local5:int = int(Math.min(_local3.length, _local4.length));
            var _local6:Array = [];
            var _local7:int;
            while (_local7 < _local5) {
                if (_local3[_local7] == _local4[_local7]){
                    _local6.push(_local3[_local7]);
                } else {
                    break;
                };
                _local7++;
            };
            if (!_local6.length){
                return (null);
            };
            return (_local6);
        }
        public static function stringByAppendingPathComponent(_arg1:String, _arg2:String):String{
            if (_arg2.indexOf("/") == 0){
                _arg2 = _arg2.substr(0, (_arg2.length - 1));
            };
            if (_arg1.lastIndexOf("/") != (_arg1.length - 1)){
                _arg1 = (_arg1 + "/");
            };
            return ((_arg1 + _arg2));
        }
        public static function lastPathComponent(_arg1:String):String{
            var _local2:int = ((_arg1.charAt((_arg1.length - 1)) == "/")) ? (_arg1.length - 2) : (_arg1.length - 1);
            return (((_local2 == -1)) ? "" : _arg1.substring((_arg1.lastIndexOf("/", _local2) + 1), (_local2 + 1)));
        }
        public static function pathExtension(_arg1:String):String{
            if (_arg1 == null){
                return ("");
            };
            var _local2:int = _arg1.lastIndexOf(".");
            if (_local2 == -1){
                return ("");
            };
            var _local3:int = _arg1.lastIndexOf("/");
            if (((!((_local3 == -1))) && ((_local2 < _local3)))){
                return ("");
            };
            return (_arg1.substring((_local2 + 1)));
        }
        public static function pathComponents(_arg1:String):Array{
            var _local2:Array = _arg1.split("/");
            if (!_local2.length){
                return (_local2);
            };
            if (_local2[0].length == 0){
                _local2[0] = "/";
            };
            var _local3:int = _local2.length;
            while (_local3-- > 1) {
                if (_local2[_local3].length == 0){
                    _local2.splice(_local3, 1);
                };
            };
            return (_local2);
        }
        public static function stringByResolvingRelativePathToPath(_arg1:String, _arg2:String=null):String{
            var _local7:String;
            _arg1 = stringByStandardizingPath(_arg1);
            if (!_arg2){
                return (_arg1);
            };
            _arg2 = stringByStandardizingPath(_arg2);
            if (isAbsolutePath(_arg1)){
                return (_arg1);
            };
            var _local3:Array = pathComponents(_arg1);
            var _local4:Array = pathComponents(_arg2);
            var _local5 = "";
            if (pathExtension(_arg1) != ""){
                _local5 = String(_local3.pop());
            };
            var _local6:int;
            while (_local6 < _local3.length) {
                _local7 = _local3[_local6];
                if (_local7 == ".."){
                    _local4.pop();
                } else {
                    _local4.push(_local7);
                };
                _local6++;
            };
            _local4.push(_local5);
            return (stringByStandardizingPath(pathWithComponents(_local4)));
        }

    }
}//package reprise.utils 
